package com.mage.completeproject.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.LocaleResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Locale;

//使用WebMvcConfigurerAdapter可以来扩展SpringMVC的功能
//@EnableWebMvc   如果使用这项注解，SpringMVC源码的配置全部失效，只有下面自己写的配置会生效
@Configuration
public class MyMvcConfig {
/**
 * 如果以下面5行代码形式来实现映射需要使本类MyMvcConfig继承WebMvcConfigurer接口，即加上implements WebMvcConfigurer,
 * 但是最好不要用这种方式，在自定义配置类MyMvcConfig中最好固定以下规范，所有的配置都用@Bean来实现
 */
//    @Override
//    public void addViewControllers(ViewControllerRegistry registry) {
//        //浏览器发送 /mage 请求,来到 success
//        registry.addViewController("/mage").setViewName("success");
//    }

    //所有的WebMvcConfigurer组件都会一起起作用
    @Bean //将组件注册在容器
    public WebMvcConfigurer webMvcConfigurer(){
        //下面创建WebMvcConfigurer接口对象的方式可以直接return不用写出adapter，即return new WebMvcConfigurer(){实现接口方法}
        return new WebMvcConfigurer() {

            //注册拦截器
            @Override
            public void addInterceptors(InterceptorRegistry registry) {
                //super.addInterceptors(registry);
                //静态资源；  *.css , *.js
                //SpringBoot已经做好了静态资源映射
//                registry.addInterceptor(new LoginHandlerInterceptor()).addPathPatterns("/**")
//                        .excludePathPatterns("/index.html","/","/user/login","/asserts/**","/webjars/**");
            }

            @Override
            public void addViewControllers(ViewControllerRegistry registry) {
                registry.addViewController("/").setViewName("login");
                registry.addViewController("/index.html").setViewName("login");
                registry.addViewController("/main.html").setViewName("dashboard");
            }
        };
    }

    @Bean
    public LocaleResolver localeResolver(){
        return new LocaleResolver() {
            @Override
            public Locale resolveLocale(HttpServletRequest httpServletRequest) {
                String l = httpServletRequest.getParameter("l");
                Locale locale = Locale.getDefault();
                if(!StringUtils.isEmpty(l)){
                    String[] split = l.split("_");
                    locale = new Locale(split[0],split[1]);
                }
                return locale;
            }

            @Override
            public void setLocale(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Locale locale) {

            }
        };
    }

    //下面的代码用以处理tomcat的如下问题：
    //INFO 13956 --- [nio-8083-exec-1] o.apache.tomcat.util.http.parser.Cookie  : A cookie header was received [1599744829,1599788998] that contained an invalid cookie. That cookie will be ignored.
    //Note: further occurrences of this error will be logged at DEBUG level.
//    @Bean
//    public WebServerFactoryCustomizer<TomcatServletWebServerFactory> cookieProcessorCustomizer() {
//        return tomcatServletWebServerFactory -> tomcatServletWebServerFactory.addContextCustomizers((TomcatContextCustomizer) context -> {
//            context.setCookieProcessor(new LegacyCookieProcessor());
//        });
//    }
}
